import Axios from 'axios'

const state = {
    tableData: [],              //表格数据
    searchTableData: [],        //拼音搜索的结果
    multipleSelection: [],      //选中表格的数据
    selectRowId: [],            //选中的表格数据id
    rowId: null,                //修改数据的id
}

const mutations = {
    updateTableData(state, arr) {           //更新表格数据
        if(Object.prototype.toString.call(arr) !== '[object Array]') return;
        let tableData = state.tableData;
        tableData.length = 0;
        arr.length > 0 ? tableData.splice(0, 1, ...arr) : tableData.splice(0);
    },
    updateSearchTableData(state, arr) {
        if(Object.prototype.toString.call(arr) !== '[object Array]') return;
        let searchTableData = state.searchTableData;
        searchTableData.length = 0;
        arr.length > 0 ? searchTableData.splice(0, 1, ...arr) : searchTableData.splice(0);
    },
    updateRowId(state, id) {
        state.rowId = id ? id : null;
    },
    multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    }
}

const actions = {
    getTableData({commit, dispatch, rootState}) {            //获取表格数据
        Axios.get('closeAccount/findCloseAccount?systemId=' + rootState.systemId).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    commit('updateTableData', res.data.data);
                    dispatch('getSearchResultByKeyWords');
                }
            }
        });
    },
    getSearchResultByKeyWords({commit}) {       //模糊拼音搜索
        Axios.get('closeAccount/findAllByName').then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    commit('updateSearchTableData', res.data.data);
                }
            }
        });
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}